package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.admin.AdminConstants;
import com.heima.common.exception.CustException;
import com.heima.feigns.ArticleFeign;
import com.heima.feigns.WemediaFeign;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDTO;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {
    @Autowired
    WemediaFeign wemediaFeign;
    @Autowired
    ArticleFeign articleFeign;

    @Autowired
    ApUserRealnameMapper mapper;


    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public ResponseResult updateStatus(AuthDTO dto, Short authStatus) {
        if (dto==null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname apUserRealname = mapper.selectOne(new LambdaQueryWrapper<ApUserRealname>().eq(ApUserRealname::getId,dto.getId()).eq(ApUserRealname::getStatus, AdminConstants.WAIT_AUTH));
        if (apUserRealname == null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (authStatus == 2){
            apUserRealname.setStatus(authStatus);
            apUserRealname.setReason(dto.getMsg());
            mapper.updateById(apUserRealname);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        ApUser apUser = getById(apUserRealname.getUserId());
        if (apUser == null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (authStatus == 9){
            apUserRealname.setStatus(authStatus);
            apUserRealname.setUpdatedTime(new Date());
            mapper.updateById(apUserRealname);
            WmUser wmUser = addWeMedia(apUser);
            addAuthor(wmUser);
        }
        apUser.setFlag((short) 1);
        updateById(apUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    private void addAuthor(WmUser wmUser) {
        ResponseResult<ApAuthor> responseResult = articleFeign.findByUserId(wmUser.getApUserId());
        if (responseResult.getCode() != 0){
            CustException.cust(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
        }
        ApAuthor apAuthor = responseResult.getData();
        if (apAuthor != null){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST);
        }
        apAuthor = new ApAuthor();
        apAuthor.setCreatedTime(new Date());
        apAuthor.setName(wmUser.getName());
        apAuthor.setType(2); // 自媒体人类型
        apAuthor.setUserId(wmUser.getApUserId()); // APP 用户ID
        apAuthor.setWmUserId(wmUser.getId()); // 自媒体用户ID
        articleFeign.saveUser(apAuthor);
    }

    private WmUser addWeMedia(ApUser apUser) {
        ResponseResult<WmUser> responseResult = wemediaFeign.findByName(apUser.getName());
        if (responseResult.getCode() != 0){
            CustException.cust(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
        }
        WmUser wmUser = responseResult.getData();
        if (wmUser != null){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST);
        }
        wmUser = new WmUser();
        BeanUtils.copyProperties(apUser,wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setCreatedTime(new Date());
        wmUser.setNickname(apUser.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);
        ResponseResult<WmUser> save = wemediaFeign.save(wmUser);
        return save.getData();
    }


}
